// pages/quickPayment/quickPayment.js
const accountService = require('../../service/accountService.js');
const addressService = require('../../service/addressService.js');
const utils = require('../../utils/util.js');
// load promise 
require('../../utils/wx-pro.js');
var markersData = {

  latitude: '',//纬度

  longitude: '',//经度

  key: "90c8f19695c4f355136cf3fb2e61c023"//你申请的高德地图key

};
import payItme from '../../components/pay/pay';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: true,
    offlineCash: '',
    shopAddressList: [],
    selectShop: {
      shopId: '',
      shopName: '',
      shopAddress: ''
    },
    showBox: false,
    payMethods: [
      {
        icon: '../../images/weixin.png',
        key: '1',
        value: '微信支付'
      },
      {
        icon: '../../images/account.png',
        key: '4',
        value: '余额支付'
      }
    ],
    showPayBox: false,
    payType: '',
    codeText: '发送验证码',
    disableButton: false,
    time: 60,
    smsCode: '',
    payDelay: false,
    showToast: false,
    toastMsg: ''
  },

  //错误提示
  showToastFuc(msg) {
    this.setData({
      showToast: true,
      toastMsg: msg
    })
    setTimeout(() => {
      this.setData({
        showToast: false,
        toastMsg: ''
      })
    }, 2000)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获得dialog组件
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.location()
  },
  location: function () {
    var that = this;
    //用微信小程序的api接口获取经度，维度
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        markersData.latitude = res.latitude,
        markersData.longitude = res.longitude
        that.getShopList()
      }
    })
  },
  getShopList: function () {
    var params = {
      longitude: markersData.longitude,
      latitude: markersData.latitude
    }
    addressService.getShopAddressList(params).then(data => {
      if (data.code == 200) {
        this.setData({
          shopAddressList: data.response.listSortShops,
          selectShop: {
            shopId: data.response.listSortShops[0].id,
            shopName: data.response.listSortShops[0].name,
            shopAddress: data.response.listSortShops[0].shopAddress
          }
        })
      } else {
        this.showToastFuc(data.message)
      }
    })
      .catch(() => {
        console.log('error')
      })
  },
  accountChange: function (e) {
    var that = this
    that.setData({
      offlineCash: e.detail.value
    })
    if (that.data.offlineCash && that.data.offlineCash > 0){
      that.setData({
        disabled : false
      })
    }else{
      that.setData({
        disabled: true
      })
    }
  },
  hideBoxFun(){
    this.setData({
      showBox: false
    })
  },
  showBoxFun(){
    this.setData({
      showBox: true
    })
  },
  selectShop(event){
    console.log(event.currentTarget.dataset.shopitem)
    this.setData({
      selectShop: {
        shopId: event.currentTarget.dataset.shopitem.id,
        shopName: event.currentTarget.dataset.shopitem.name,
        shopAddress: event.currentTarget.dataset.shopitem.shopAddress
      },
      showBox: false
    })
  },
  selectPay(){
    this.setData({
      showPayBox: true
    })
  },
  closePayBox: payItme.closePayBox,
  radioChange: payItme.radioChange,
  sendCode: payItme.sendCode,
  smsCodeChange: payItme.smsCodeChange,
  confirmPay: function () {
    var _this = this
    if (!_this.data.selectShop.shopId) {
      return false
    }
    if (!_this.data.payType) {
      return false
    }
    if (_this.data.payType == 3 && _this.data.smsCode == '') {
      console.log(_this.data.smsCode)
      return false
    }
    _this.setData({
      payDelay: true
    })
    if (_this.data.payType == 1){
      var params = { shopId: _this.data.selectShop.shopId, payType: _this.data.payType, cash: _this.data.offlineCash}
      accountService.xcxOffLinePay(params).then(data => {
        _this.setData({
          payDelay: false
        })
        if (data.code == 200) {
          wx.requestPayment({
            'timeStamp': data.response.payInfo.timeStamp,
            'nonceStr': data.response.payInfo.nonceStr,
            'package': data.response.payInfo.package,
            'signType': data.response.payInfo.signType,
            'paySign': data.response.payInfo.paySign,
            'success': function (res) {
              wx.showToast({
                title: '支付成功',
                icon: 'succes',
                duration: 1000
              })
              console.log(res)
              setTimeout(() => {
                _this.setData({
                  showPayBox: false,
                })
                wx.redirectTo({
                  url: '../userOrder/userOrder?type=2,3'
                })
              }, 500)
            },
            'fail': function (res) {
              console.log(res)
            }
          })

        } else {
          _this.showToastFuc(data.message)
        }
      })
        .catch(() => {
          console.log('error')
        })
    } else if (_this.data.payType == 4){
      var params = { shopId: _this.data.selectShop.shopId, payType: _this.data.payType, cash: _this.data.offlineCash, smsCode: _this.data.smsCode}
      accountService.xcxOffLinePay(params).then(data => {
        _this.setData({
          payDelay: false
        })
        if (data.code == 200) {
          wx.showToast({
            title: '支付成功',
            icon: 'succes',
            duration: 1000
          })
          setTimeout(() => {
            _this.setData({
              showPayBox: false,
            })
            wx.redirectTo({
              url: '../userOrder/userOrder?type=2,3'
            })
          }, 500)
        } else {
          _this.showToastFuc(data.message)
        }
      })
      .catch(() => {
        console.log('error')
      })
    }
  },
})